<?php

    function codigo($length=16)
    {
        $alfa = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        $len = strlen($alfa);
        $pass = '';
        mt_srand(10000000*(double)microtime());
        for ($i = 0; $i < $length; $i++)
        {
          $pass .= $alfa[mt_rand(0,$len - 1)];
        }
        return $pass;

    }
    
    include("conecta.php");
    
    do {
        $cod = codigo();
        $sql = "SELECT * FROM documento WHERE codigo='$cod'";
        $result = mysql_query($sql);;
        $count = mysql_num_rows($result);
    } while($count == 1);
    
    return $cod;

?>